package com.sfl.api_backend.utils.task2json;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sfl.api_backend.entity.*;
import com.sfl.api_backend.entity.pred.batchTaskPred;
import com.sfl.api_backend.service.IDatasetsService;
import com.sfl.api_backend.service.IImgsService;
import com.sfl.api_backend.service.IModelsService;
import com.sfl.api_backend.service.IScriptsService;
import com.sfl.api_backend.utils.RandomID;
import com.sfl.api_backend.utils.Redis.RedisUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author szc
 * @create 2023/2/28 22:02
 */
@Component
public class Task2Json {

    @Resource
    private IModelsService modelsService;

    @Resource
    private IDatasetsService datasetsService;

    @Resource
    private IImgsService imgsService;

    @Resource
    private IScriptsService scriptsService;

    @Resource
    private RedisUtil redisUtil;

    @Value("${file.upload.path}")
    private String uploadPath;

    public String batchTask2Json(Task task) {
        batchTaskPred btp = new batchTaskPred();

        // userID
        btp.setUserId(task.getUserId());

        // taskId
        btp.setTaskId(task.getTaskId());

        // modelPath
        System.out.printf(task.getModelId().toString());

        Wrapper<Models> modelsWrapper = new LambdaQueryWrapper<>();
        ((LambdaQueryWrapper<Models>) modelsWrapper).eq(Models::getModelId, task.getModelId());
        String modelPath = modelsService.list(modelsWrapper).get(0).getModelPath();
        btp.setModelPath(modelPath);

        // imgPaths
        Datasets ds = datasetsService.getById(task.getImgsetId());
        String imgsetPathRoot = "";
        if (ds.getImgsetId() != null) {
            imgsetPathRoot = ds.getImgsetPath();
        }
        else {
            throw new RuntimeException("imgsetPathRoot is null");
        }
        File imgsetPathRootFile = new File(imgsetPathRoot);
        File[] imgsetPathRootFiles = imgsetPathRootFile.listFiles();
        List<String> imgPaths = new ArrayList<>();
        if (imgsetPathRootFiles != null) {
            for (File imgsetPathRootFile1 : imgsetPathRootFiles) {
                imgPaths.add(imgsetPathRootFile1.getAbsolutePath());
            }
        }

        btp.setImgPaths(imgPaths);

        // imgIds
        Wrapper<Imgs> imgWra = new LambdaQueryWrapper<>();
        ((LambdaQueryWrapper<Imgs>) imgWra).eq(Imgs::getImgsetId, task.getImgsetId());
        List<Imgs> imgs = imgsService.list(imgWra);
        List<Integer> imgIds = new ArrayList<>();
        for (Imgs img : imgs) {
            imgIds.add(img.getImgId());
        }
        btp.setImgIds(imgIds);

//        // exScriptPath
//        Scripts exScript = scriptsService.getById(task.getExScriptId());
//        btp.setExScriptPath(exScript.getScriptPath());
//
//        // backScriptPath
//        Scripts backScript = scriptsService.getById(task.getBackScriptId());
//        btp.setBackScriptPath(backScript.getScriptPath());

        // savePath
        String savePath = uploadPath + task.getUserId() + "/taskTmp/" + task.getTaskId() + "/" ;
        btp.setSavePath(savePath);

//        // taskConfig
//        String taskConfig = redisUtil.get("taskConfig:" + task.getTaskId(), String.class);
//        btp.setBackClass(taskConfig);

        String userRoot = uploadPath + task.getUserId() + "/";

        Integer labelSetId = task.getLabelSetId();

        btp.setUserRoot(userRoot);
        btp.setLabelSetId(labelSetId.toString());

        return JSON.toJSONString(btp);
    }

}
